/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 *
 * @author yaoyuming
 *
 * @date 2019-11-19 16:59
 */
package com.yao.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.yao.core.Api;
import com.yao.core.Column;
import com.yao.core.Table;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 解析word文档
 *
 * @author yaoyuming
 * @date 2019/11/19 16:59
 */
public class WordUtil {

    private static final Log log = LogFactory.get();

    private static List<Table> tables = CollectionUtil.newArrayList();

    public static List<Table> create(XWPFDocument doc) {
        table(doc);
        column(doc);
        return tables;
    }

    private static void table(XWPFDocument doc) {
        tables = doc.getParagraphs().stream().filter(e -> StrUtil.isNotBlank(e.getText())).map(e -> {
            String tableName = StrUtil.subBefore(e.getText(), "（", false);
            String tableComment = StrUtil.subBetween(e.getText(), "（", "）");
            return Table.builder().tableName(StrUtil.trim(tableName)).tableComment(tableComment).build();
        }).collect(Collectors.toList());
    }

    private static void column(XWPFDocument doc) {
        List<XWPFTable> docTables = doc.getTables();

        for (int i = 0; i < docTables.size(); i++) {
            List<XWPFTableRow> rows = docTables.get(i).getRows();
            Table table = tables.get(i);
            List<Column> columns = CollectionUtil.newArrayList();
            for (int j = 0; j < rows.size(); j++) {
                if (j == 0) {
                    // 表名称
                    String manage = StrUtil.trim(rows.get(j).getTableCells().get(0).getText());
                    log.info("管理名称 = " + manage);
                    Api api = Api.builder().tag(manage).apiOperationValue(StrUtil.subBefore(manage, "管理", false)).build();
                    table.setApi(api);
                    continue;
                }
                if (j == 1) {
                    continue;
                }
                List<XWPFTableCell> cells = rows.get(j).getTableCells();
                Column.ColumnBuilder builder = Column.builder();
                for (int k = 0; k < cells.size(); k++) {
                    XWPFTableCell cell = cells.get(k);

                    switch (k) {
                        case 0:
                            builder.name(StrUtil.trim(cell.getText()));
                            break;
                        case 1:
                            builder.type(StrUtil.trim(cell.getText()));
                            break;
                        case 2:
                            builder.nullAble(StrUtil.trim(cell.getText()));
                            break;
                        case 3:
                            builder.comment(StrUtil.trim(cell.getText()));
                            break;
                        case 4:
                            builder.defaultValue(StrUtil.trim(cell.getText()));
                            break;
                        default:
                            break;
                    }
                }
                columns.add(builder.build());
            }
            table.setColumnList(columns);
        }
    }

}